from Block import Block
import time
from Crypto.Hash import SHA256
import Storage
import BlockVerifier


'''打包'''
def seal(txs, block_number, sealer_id):
    block = Block(number=block_number)
    block.blockBody.transactions = txs
    temp = SHA256.new()
    for tx in txs:
        temp.update(bytes.fromhex(tx.hashWith))
    block.blockHeader.dbHash = temp.hexdigest()
    block.blockHeader.sealer = sealer_id
    block.blockHeader.sealerList.append(sealer_id)
    block.blockHeader.sealerList = list(set(block.blockHeader.sealerList))
    block.blockHeader.timestamp = int(time.time() * 10000000)
    return block


'''执行'''
def execute(block):
    return BlockVerifier.execute(block)


'''存储'''
def store(block, chain):
    Storage.store(block, chain)

'''共识'''
def consensus(block, chain):
    block = BlockVerifier.execute(block)
    store(block, chain)
